package com.zy.design.demo.behavioral;

/**
 * 备忘录模式
 * 特点：保存对象的状态，以便在需要时恢复。
 */
class Memento {
    private String state;

    public Memento(String state) { this.state = state; }

    public String getState() { return state; }
}

class Originator {
    private String state;

    public void setState(String state) {
        this.state = state;
        System.out.println("状态设置为: " + state);
    }

    public Memento saveState() { return new Memento(state); }

    public void restoreState(Memento memento) {
        this.state = memento.getState();
        System.out.println("状态恢复为: " + state);
    }
}

class Main3 {
    public static void main(String[] args) {
        Originator originator = new Originator();
        originator.setState("状态1");

        Memento memento = originator.saveState();

        originator.setState("状态2");
        originator.restoreState(memento);
    }
}
